package com.qf.controller;

import com.qf.pojo.Lianhe;
import com.qf.service.LianHeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/lianhe")
public class LianHeController {
    @Autowired
    private LianHeService lianHeService;

    //查询所有
    @RequestMapping("/ht")
    public  String lianhelist(Model model) {
         List<Lianhe>lianhes=lianHeService.findAlllianhe();
         model.addAttribute("lianhes",lianhes);
         return "shouye";
    }


    //随机查询联合表
    @RequestMapping("/findLianheRand")
    @ResponseBody
    List<Lianhe> findLianheRand() {
        List<Lianhe> lianheRand = lianHeService.findLianheRand();
        return lianheRand;
    }

    //按typename查询联合表
    @RequestMapping("/findLianheByTypename/{typename}")
    @ResponseBody
    List<Lianhe> findLianheByTypename(@PathVariable String typename) {
        List<Lianhe> lianheByTypename = lianHeService.findLianheByTypename(typename);
        return lianheByTypename;
    }

    //按id查询联合表
    @RequestMapping("/findById")
    public  String findById(Integer lid,Model model){
        Lianhe byId = lianHeService.findById(lid);
        model.addAttribute("lianhe",byId);
        return "update";
    }
    //修改
    @RequestMapping("/updatecontent")
    public  String updatecontent(Lianhe lianhe){
        lianHeService.updatecontent(lianhe);
        return "redirect:/lianhe/ht";
    }
    //删除
    @RequestMapping("/delcontent")
    public String delcontent(Integer lid){
        lianHeService.delcontent(lid);
        return "redirect:/lianhe/ht";
    }
    //根据id查添加
    @RequestMapping("/jia")
    public  String jia(Integer lid){
        Lianhe byId = lianHeService.findById(lid);
        return "insert";

    }
    //添加
    @RequestMapping("/addcontent")
    public String addcontent(Lianhe lianhe){
        lianHeService.addcontent(lianhe);
        return "redirect:/lianhe/ht";
    }

    //模糊查询
    @RequestMapping("/findAll/{lname}")
    @ResponseBody
    public  List<Lianhe> findAll(@PathVariable String lname,Model model){
        List<Lianhe> all = lianHeService.findAll(lname);
        model.addAttribute("all",all);
       return all;
    }
}
